from datetime import datetime
import math
import multiprocessing
import cv2
from utils.QCRb import FQCR, FQCRARR, FQCREN, FQCREN_text
from utils.action import click, click_rect, click_right, double_click, find_scale_img, is_inside, sleep_ad, wasd, window_screenshot
from utils.yolo import yl, yl_center, yl_rect

x = 0
y = 0

center_x = 0
center_y = 0

x_offset = 0
y_offset = 0

max_x_offset = 0
max_y_offset = 0
'''
x,y 是原点
x_offset, y_offset 为移动位置，相对于下，y

x_center,y_center 拿到屏幕中心点,初始化时得到
x_move,y_move 每次相对于心中移动的数值
x_move-x_center,y_center-y_move,
然后进行存储
x_offset=x_offset+(x_move-x_center)
y_offset=y_offset+(y_center-y_move)

max_x_offset,max_x_offset最大偏移值,对比时使用绝对值

使用进程执行程序
0 是怪物，2是选中怪物的X
'''

# 判断是否选中


def get_check_pig(screen):
    check_pig = FQCR(screen, '猪', 0.2, 0.45, 0, 0.12)
    if check_pig['isFind']:
        return True
    return False


def input_resuit(hwnd, screen, result):
    for digit in str(result):
        a = FQCREN(screen, text=digit, x=0.386, x1=0.716, y=0.352, y1=0.772)
        if a['isFind']:
            click(hwnd, a['x'], a['y'], 10, 10)
        sleep_ad(1, 0.5)
        print(digit)
    sleep_ad(10, 15)
    btn = FQCRARR(screen, ['确', '认'], x=0.386,
                  x1=0.716, y=0.352, y1=0.772)
    if btn['isFind']:
        click(hwnd, btn['x'], btn['y'])
        sleep_ad(0.5, 0.5)
    check_pig = get_check_pig(screen)
    if check_pig:
        click_right(hwnd, btn['x'], btn['y'], 120, 100)
        print('开始攻击')
        sleep_ad(3, 2)
        attacking(hwnd)


def get_result(screen):
    number1 = FQCREN_text(screen, 0.457, 0.49, 0.285, 0.348)
    number2 = FQCREN_text(screen, 0.52, 0.56, 0.285, 0.348)
    symbol_add = find_scale_img('./sms/add.png', 981,
                                screen, 0.8, 0.457, 0.56, 0.285, 0.348)
    symbol_sub = find_scale_img('./sms/sub.png', 983,
                                screen, 0.8, 0.457, 0.56, 0.285, 0.348)
    operator = '*'
    if symbol_add['isFind']:
        operator = '+'
    if symbol_sub['isFind']:
        operator = '-'

    # 构建表达式
    expression = number1 + operator + number2
    # 计算表达式结果
    result = eval(expression)
    print(result)
    with open('日志.txt', 'a', encoding='utf-8') as f:
        f.write(f'{datetime.now()}---辅助识别：{expression}, {result}\n')
    return result


def sms(hwnd, screen):
    yanzheng = FQCRARR(screen, ['辅', '验', '证'], 0.44, 0.70, 0.11, 0.83)

    if yanzheng['isFind']:
        click_rect(hwnd, screen, 0.539, 0.708, 0.067, 0.191)
        sleep_ad(1.5, 1.5)
        screen = window_screenshot(hwnd)
        result = get_result(screen)
        sleep_ad(1, 1)
        input_resuit(hwnd, screen, result)

# 扩展:根据进程编号杀死指定进程
# os.kill(os.getpid(), 9)


def get_center(hwnd):
    global center_x
    global center_y
    screen = window_screenshot(hwnd)
    temp = cv2.imread(screen['path'])
    tempHeight, tempWidth = temp.shape[0:2]
    center_x = tempWidth // 2
    center_y = tempHeight // 2


def lately_pig(pig=[]):
    min_distance_index = min(
        range(len(pig)), key=lambda i: math.hypot(abs(pig[i]['x']-center_x), abs(pig[i]['y']-center_y)))
    return pig[min_distance_index]


def attacking(hwnd):
    num = 0
    while True:
        sleep_ad(0.25, 0.25)
        screen = window_screenshot(hwnd)
        sms(hwnd, screen)
        num = num+1
        # 前面已经判断选中的一定是目标怪物
        check_pig = yl_rect(screen, [2], 0.2, 0.5, 0)
        if check_pig:
            if num == 100:
                click_right(hwnd, 300,
                            200, 120, 100)
                break
            print(f'正在攻击或走向怪物{num}')
        else:
            print('怪物死亡')
            break


def jissu(hwnd):
    screen = window_screenshot(hwnd)
    sms(hwnd, screen)
    pigs = yl_center(screen, [0])
    if len(pigs) == 0:
        print('加速')
        click_rect(hwnd, screen, 0.816, 0.934, 0.020, 0.036)
        num = 0
        while True:
            screen = window_screenshot(hwnd)
            sms(hwnd, screen)
            # 前面已经判断选中的一定是目标怪物
            pigs = yl_rect(screen, [2], 0.2, 0.5, 0)
            if pigs:
                print(f'加速走向怪物{num}')
                num = num+1
                if num == 40:
                    click_right(hwnd, 300,
                                200, 120, 100)
                    break
                pigs = yl_center(screen, [0])
                if len(pigs) != 0:
                    print('攻击')
                    click_right(hwnd, pigs[0]['x']+10,
                                pigs[0]['y']+30, 120, 100)
                    break
            else:
                break


def start(hwnd):
    while True:
        sleep_ad(0.25, 0.25)
        screen = window_screenshot(hwnd)
        sms(hwnd, screen)
        try:
            pigs = yl(screen, [0])
            if len(pigs) != 0:
                print('找到怪物')
                pig = lately_pig(pigs)
                if pig['isFind']:
                    # 判断是否是禁止点击区域
                    no_click = is_inside(screen, (pig['x']+10, pig['y']+30), [
                        [0, 0.233, 0, 0.199],
                        [0.02, 0.054, 0.211, 0.273],
                        [0.883, 0.966, 0, 0.098],
                        [0.324, 0.587, 0.8, 0.907],
                        [0.263, 0.587, 0.912, 1],
                        [0.765, 1, 0.699, 1]])
                    if no_click:
                        continue
                    double_click(hwnd, pig['x']+10, pig['y']+20,)
                    sleep_ad(0.25, 0.25)
                    # 判断是否选中怪我
                    for _ in range(2):
                        screen = window_screenshot(hwnd)
                        sms(hwnd, screen)
                        # 是否有选中目标怪物
                        check_pig = get_check_pig(screen)
                        if check_pig:
                            click_right(hwnd, pig['x']+10,
                                        pig['y']+30, 120, 100)
                            sleep_ad(0.25, 0.25)
                            print('开始攻击')
                            jissu(hwnd)
                            attacking(hwnd)
                            break
                        else:
                            screen = window_screenshot(hwnd)
                            sms(hwnd, screen)
                            # 是否有选中目标
                            pigs = yl_rect(screen, [2], 0.2, 0.5, 0)
                            if pigs['isFind']:
                                wasd(hwnd, screen)
                            else:
                                break

        except Exception:
            continue


def main(hwnd):
    get_center(hwnd)
    start(hwnd)


if __name__ == "__main__":
    # screen = window_screenshot(hwnd)
    # pigs = yl_rect(screen, [2], 0.2, 0.5, 0)
    # jissu(hwnd)
    # 创建一个进程来处理截图和识别任务
    hwnd = input('请输入：')
    capture_process = multiprocessing.Process(target=main, args=(hwnd,))
    capture_process.start()

    print("我是墨武侠")

else:
    print("我是墨武侠")
